package com.qijian.maindata.domain.vo;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qijian.common.utils.StringUtils;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@Data
public class GanttBOMDetail {

    @JsonSerialize(using = ToStringSerializer.class)
    private Long projectId;

    private String id;

    private String detailId;

    private String pid;

    private String parent;

    private String detailCode;

    private String detailItemName;

    private String detailItemCode;

    private String partNo;

    private String drawingNo;

    private String processLineCode;

    private String factoryName;

    private String label;

    //左侧 label
    private String label1;
    private String label2;
    private String label3;
    private String label4;
    private String label5;
    private String label6;

    public String getLabel1() {
        return this.getValueViaType(1);

    }

    public String getLabel2() {
        return this.getValueViaType(2);
    }

    public String getLabel3() {
        return this.getValueViaType(3);
    }

    public String getLabel4() {
        return this.getValueViaType(4);
    }

    public String getLabel5() {
        return this.getValueViaType(5);
    }

    public String getLabel6() {
        return this.getValueViaType(6);
    }

    private LocalDateTime startTime;

    private LocalDateTime endTime;

    private Integer uniqueHashCode;

    // 1.bom节点  2.车间 3.开工令 4.BOM
    private Integer type;

    public Integer getUniqueHashCode() {
        return Objects.hash(
                detailId,
                detailCode,
                detailItemName,
                detailItemCode,
                partNo,
                drawingNo,
                processLineCode,
                pid
        );
    }

    public String getLabel() {
        if (type == null) {
            return null;
        }
        // bom
        if (type == 1) {
            if (StringUtils.isEmpty(processLineCode)) {
                return this.detailItemCode + "." + this.detailCode + " " + detailItemName;
            }
            return this.detailItemCode + "." + this.detailCode + " " + detailItemName + " " + processLineCode;
        }
        if (type == 3) {
            return this.detailItemCode;
        }
        if (type == 4) {
            return this.detailCode;
        } else {
            return this.factoryName;
        }
    }


    public String getValueViaType(int index) {
        if (type == null) {
            return null;
        }
        if (index == -1) {
            if (processLineCode == null) {
                return detailItemCode + "." + detailCode + " " + detailItemName;
            } else {
                return detailItemCode + "." + detailCode + " " + detailItemName + " " + processLineCode;
            }
        }
        // bom
        if (type == 1) {
            switch (index) {
                case 1:
                    return this.detailItemCode + "." + detailCode;
                case 2:
                    return this.detailItemName;
                case 3:
                    return this.drawingNo;
                case 4:
                    return this.getPartNo();
                case 5:
                    return this.processLineCode;
            }
        } else if (type == 2) {
            switch (index) {
                case 3:
                    return this.factoryName;
                case 4:
                    return getDate(this.startTime);
                case 5:
                    return getDate(this.endTime);
                default:
                    return null;
            }
        } else if (type == 3) {
            switch (index) {
                case 1:
                    return this.getDetailCode();
                case 2:
                    return this.getDetailItemCode();
                case 4:
                    return getDate(this.startTime);
                case 5:
                    return getDate(this.endTime);
                default:
                    return null;
            }
        } else if (type == 4) {
            switch (index) {
                case 1:
                    return this.getDetailCode();
                default:
                    return null;
            }
        }
        return null;
    }

    public String getDate(LocalDateTime time) {
        if (null == time || (type == 1 && processLineCode == null)) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return time.format(formatter);
    }
}
